package com.yzc.mihuyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzc.mihuyou.entity.Plate;
import com.yzc.mihuyou.mapper.PlateMapper;
import com.yzc.mihuyou.service.PlateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 易紫诚
 * @since 2022-04-29
 */
@Service
public class PlateServiceImpl extends ServiceImpl<PlateMapper, Plate> implements PlateService {
    @Autowired
    PlateMapper plateMapper;

    /**
     * 批量导入专业信息
     *
     * @param file
     */
    public Map<String, Object> importProfession(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
// 获取文件的名称
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
// 获取文件的后缀名
        String pattern = fileName.substring(fileName.lastIndexOf(".") + 1);
        System.out.println(pattern);
        List<List<String>> listContent = new ArrayList<>();
        String message = "导入成功";
        try {
            if (file != null) {
                //文件类型判断
                if (!ExcelUtil.isEXCEL(file)) {
                    message = "该文件不是excel文件";
                } else {
                    listContent = ExcelUtil.readExcelContents(file, pattern,2);
                    //文件内容判断
                    if (listContent.isEmpty()) {
                        message = "表格内容为空";
                    } else {
                        // 循环遍历
                        for (int i = 0; i < listContent.size(); i++) {
                            Plate plate = new Plate();
                            String name = listContent.get(i).get(0);
                            String imafe = listContent.get(i).get(1);
                            //赋值
                            plate.setPlateName(name);
                            plate.setPlateImg(imafe);
                            plateMapper.insert(plate);
                        }
                    }
                }
            } else {
                message = "未选择文件";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("code", 200);
        map.put("msg", message);
        map.put("data", fileName);
        return map;
    }

    @Override
    public List<Plate> plateList() {
        return plateMapper.selectList(null);
    }


}
